package demo2.util;

import demo2.util.ai.AISearcher;

import java.util.Set;

public class AIGameHandler implements GameHandler {
    private GameHandler nextHandler;
    private Set<String> solutions;

    @Override
    public void handleRequest() {
        if (solve()) {


            return;
        }
        if (nextHandler != null) {
            nextHandler.handleRequest();
        }

    }

    @Override
    public void setNextHandler(GameHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    /**
     * 全排列或者优化的暴力搜索器实现
     *
     * @return
     */
    @Override
    public boolean solve() {
        solutions = new AISearcher().searchAll(new int[]{Game.getInstance().getDigitAt(0),
                Game.getInstance().getDigitAt(1),
                Game.getInstance().getDigitAt(2),
                Game.getInstance().getDigitAt(3),
        });


        Game.getInstance().setSolution(solutions);
        return !Game.getInstance().getSolution().isEmpty();

    }
}